import sqlite3
import os
from datetime import datetime


def get_chrome_download_history():
    # Chrome下载历史数据库文件路径
    chrome_history_path = r"H:\download\chrome\profile\Default\History"

    # 连接数据库
    connection = sqlite3.connect(chrome_history_path)
    cursor = connection.cursor()

    # 获取表格中的列名
    cursor.execute("PRAGMA table_info(downloads)")
    columns = cursor.fetchall()

    # 打印列名
    print("Column Names:")
    for column in columns:
        print(column[1])

    # 获取下载记录
    cursor.execute("""
        SELECT downloads.target_path,url_chains.*
        FROM downloads
        LEFT JOIN downloads_url_chains AS url_chains ON downloads.id = url_chains.id
        ORDER BY downloads.start_time DESC
        """)

    rows = cursor.fetchall()
    # 关闭连接
    cursor.close()
    connection.close()

    # 打印下载记录
    print(
        "ID\tURL\t\t\t\t\t\t\t\t\t\t\t\t\tPath\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tStart Time")
    print("=" * 150)
    for row in rows:
        print(row)
        # download_id, site_url, target_path, start_time = row
        # start_time_str = datetime.fromtimestamp(start_time).strftime('%Y-%m-%d %H:%M:%S')
        # print(f"{download_id}\t{site_url}\t{target_path}\t{start_time_str}")


if __name__ == "__main__":
    get_chrome_download_history()
